﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCMS.Model;
using System.Data.Objects.DataClasses;

namespace SCMS.CoreBusinessLogic
{
    public class VehicleMakeService : IVehicleMakeService
    {
        public bool IsVMakeSaved(VehicleMake entity)
        {
            using (var db = new SCMSEntities())
            {
                db.VehicleMakes.AddObject(entity);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsMakeEdited(VehicleMake entity)
        {
            using (var db = new SCMSEntities())
            {
                db.VehicleMakes.Attach(entity);
                db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public VehicleMake GetMakeById(Guid makeId)
        {
            using (var db = new SCMSEntities())
            {
                return db.VehicleMakes.FirstOrDefault(p => p.Id == makeId);
            }
        }

        public List<VehicleMake> GetVMakes(Guid cpId)
        {
            using (var db = new  SCMSEntities())
            {
                return db.VehicleMakes.Where(p => p.CountryProgrammeId == cpId).ToList();
            }
        }

        public bool IsVMakeDeleted(Guid makeId)
        {
            using (var db = new SCMSEntities())
            {
                db.VehicleMakes.DeleteObject(db.VehicleMakes.FirstOrDefault(p => p.Id == makeId));
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsVModelSaved(Model.VehicleModel entity)
        {
            using (var db = new SCMSEntities())
            {
                db.VehicleModels.AddObject(entity);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsModelEdited(Model.VehicleModel entity)
        {
            using (var db = new SCMSEntities())
            {
                db.VehicleModels.Attach(entity);
                db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public Model.VehicleModel GetModelById(Guid modelId)
        {
            using (var db = new SCMSEntities())
            {
                return db.VehicleModels.FirstOrDefault(p => p.Id == modelId);
            }
        }

        public bool IsVModelDeleted(Guid modelId)
        {
            using (var db = new SCMSEntities())
            {
                db.VehicleModels.DeleteObject(db.VehicleModels.FirstOrDefault(p => p.Id == modelId));
                return db.SaveChanges() > 0 ? true : false;
            }
        }
    }
}
